package setting

import (
	"time"
)

type ServerSettingS struct {
	RunMode      string
	HttpPort     string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

type AppSettingS struct {
	DefaultPageSize       int
	MaxPageSize           int
	DefaultContextTimeout time.Duration
	LogSavePath           string
	LogFileName           string
	LogFileExt            string
	UploadVideoSavePath   string
	UploadImageSavePath   string
	UploadVideoServerUrl  string
	UploadImageServerUrl  string
	UploadImageMaxSize    int
	UploadImageAllowExts  []string
}

type JWTSettingS struct {
	Secret string
	Issuer string
	Expire time.Duration
}

type DatabaseSettingS struct {
	DBType                 string
	UserName               string
	Password               string
	Host                   string
	DBName                 string
	TablePrefix            string
	Charset                string
	ParseTime              bool
	MaxIdleConns           int
	MaxOpenConns           int
	PrepareStmt            bool
	SkipDefaultTransaction bool
}

type RedisSettingS struct {
	EndPoint string
	PoolSize int
	Database int
	PassWord string
	Prefix   string
}
type LimiterSettingS struct {
	ContextTimeOut time.Duration
}

var sections = make(map[string]interface{})

func (s *Setting) ReadSection(k string, v interface{}) error {
	err := s.vp.UnmarshalKey(k, v)
	if err != nil {
		return err
	}

	if _, ok := sections[k]; !ok {
		sections[k] = v
	}
	return nil
}

func (s *Setting) ReloadAllSection() error {
	for k, v := range sections {
		err := s.ReadSection(k, v)
		if err != nil {
			return err
		}
	}

	return nil
}
